﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace centraldba_library.Admin
{
    public class target_group : INotifyPropertyChanged
    {
        private string _group_name;
        Int32 _server_group_id;
        msx_server _parent;

        #region " Constructor "

        public target_group(msx_server MSXServer)
        {
            _parent = MSXServer;
            _parent.PropertyChanged += new PropertyChangedEventHandler(_parent_PropertyChanged);
        }

        #endregion

        #region " Events "

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        void _parent_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("all_servers"))
                NotifyPropertyChanged("member_servers");
        }

        #endregion

        #region " Properties "

        public msx_server Parent
        {
            get { return _parent; }
        }

        public string group_name
        {
            get { return this._group_name; }
            set
            {
                if (this._group_name != value)
                {
                    this._group_name = value;
                    NotifyPropertyChanged("group_name");
                }
            }
        }

        public Int32 server_group_id
        {
            get { return this._server_group_id; }
            set
            {
                if (this._server_group_id != value)
                {
                    this._server_group_id = value;
                    NotifyPropertyChanged("server_group_id");
                }
            }
        }

        public ObservableCollection<target_server> member_servers
        {
            get 
            {
                var query = from x in _parent.all_servers
                            where x.server_group_id.Equals(_server_group_id)
                            select x;

                //return (ObservableCollection<target_server>) query;
                return new ObservableCollection<target_server>(query);
            }
        }

        #endregion

    }
}
